package com.tanhua.mangement.controller;

import com.tanhua.dubbo.server.pojo.Movements;

import com.tanhua.mangement.service.MessagesManageService;
import com.tanhua.mangement.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


//      /manage/messages
@RestController
@RequestMapping("manage")
@Slf4j
public class MessagesManageController {
    @Autowired
  private MessagesManageService messagesManageService;

    /**
     * 查状态
     * @param pagesize
     * @param page
     * @param sortProp
     * @param sortOrder
     * @param id
     * @param sd
     * @param ed
     * @param uid
     * @param state
     * @return
     */
    @GetMapping("messages")
    public ResponseEntity<PageResult> queryUserPublish(@RequestParam Integer pagesize,
                                                       @RequestParam Integer page,
                                                       @RequestParam String sortProp,
                                                       @RequestParam String sortOrder,
                                                       @RequestParam(required = false) String id,
                                                       @RequestParam(required = false) String sd,
                                                       @RequestParam(required = false)String ed,
                                                       @RequestParam(required = false) String uid,
                                                       @RequestParam(required = false) String state) {

        PageResult pageResult = null;
        log.info("接收uid:{}",uid);
        try {
            pageResult = messagesManageService.queryUserPublish(pagesize, page, sortProp, sortOrder, id, sd, ed,uid, state);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(pageResult);
    }

//    根据id查询详情
    @GetMapping("messages/{id}")
    public ResponseEntity<Movements> queryPublishById(@PathVariable("id")  String  id) {
//        如果查到就不返回null否则返回
        Movements movements = null;
        try {
            movements = messagesManageService.queryPublishById(id);
            if (movements != null) {
                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(movements);
    }

    /**
     * 动态置顶
     * @return
     */
    @PostMapping("messages/{id}/top")
    public ResponseEntity<String> top(@PathVariable("id") String id) {
        try {
            Boolean result =  messagesManageService.top(id);
            if (result) {
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");
    }

    @PostMapping("messages/{id}/untop")
    public ResponseEntity<String> unTop(@PathVariable("id") String id) {
        try {
            Boolean result =  messagesManageService.unTop(id);
            if (result) {
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");
    }

    /**
     * 通过
     * @param ids
     * @return
     */
    @PostMapping("messages/pass")
    public ResponseEntity<String> messagePass(@RequestBody String[] ids) {
        try {
            Boolean b = messagesManageService.messagePass(ids);
            if (b) {
                return ResponseEntity.ok("审核通过");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("审核出错");
    }

    /**
     * 拒绝
     * @param ids
     * @return
     */
    @PostMapping("messages/reject")
    public ResponseEntity<String> messageReject(@RequestBody String[] ids) {
        try {
            Boolean b = messagesManageService.messageReject(ids);
            if (b) {
                return ResponseEntity.ok("审核通过");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("审核出错");
    }

    /**
     * 撤销
     * @param ids
     * @return
     */
    @PostMapping("messages/revocation")
    public ResponseEntity<String> messageRevocation(@RequestBody String[] ids) {
        try {
            Boolean b = messagesManageService.messageRevocation(ids);
            if (b) {
                return ResponseEntity.ok("审核通过");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("审核出错");
    }

}
